var get = "GET";
var url = "https://edu.telking.com/api/";
var month = "?type=month";
var week = "?type=week";
var option = {
    legend: {
    },
    xAxis: {
    },
    yAxis: {},
    series: [
        {
            type: 'line',
            smooth: true,
        }
    ]
};

var option2 = {
    series: [
        {
            type: 'pie',
            data: []
        }
    ]
};

var option3 = {
    legend: {
    },
    xAxis: {
    },
    yAxis: {},
    series: [
        {
            type: 'bar',
        }
    ]
};


var ajaxHttpRequestFunc = new Promise(function (resolve, reject) {
    var xmlHttpRequest;
    if (window.ActiveXObject) {
        xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.XMLHttpRequest) {
        xmlHttpRequest = new XMLHttpRequest();
    }
    xmlHttpRequest.open(get, url + month, true);
    xmlHttpRequest.send(null); // 发送请求
    xmlHttpRequest.onreadystatechange = function () {
        if (xmlHttpRequest.readyState == 4) {
            console.log('状态码为', xmlHttpRequest.status);
            if (xmlHttpRequest.status == 200) {
                var object = JSON.parse(xmlHttpRequest.responseText);
                option.xAxis["data"] = object.data.xAxis;
                option.series[0]['data'] = object.data.series;
                resolve();
            } else {
                alert("error:HTTP状态码为:" + xmlHttpRequest.status);
                reject();
            }
        }
    }
})

ajaxHttpRequestFunc.then(function () {
    var myChart = echarts.init(document.getElementById('main-month'));
    myChart.setOption(option);
})

var ajaxHttpRequestFunc = new Promise(function (resolve, reject) {
    var xmlHttpRequest;
    if (window.ActiveXObject) { // IE浏览器的创建方式
        xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.XMLHttpRequest) {
        xmlHttpRequest = new XMLHttpRequest();
    }
    xmlHttpRequest.open(get, url + week, true); // 创建http请求，并指定请求得方法（get）、url（https://www.runoob.com/try/ajax/ajax_info.txt）以及验证信息
    xmlHttpRequest.send(null); // 发送请求
    xmlHttpRequest.onreadystatechange = function () {
        if (xmlHttpRequest.readyState == 4) {
            console.log('状态码为', xmlHttpRequest.status);
            if (xmlHttpRequest.status == 200) {
                var object = JSON.parse(xmlHttpRequest.responseText);
                var series = object.data.series;
                var xAxis = object.data.xAxis;
                const f1 = (key, arr) => arr.map(item => ({
                    [key]: item
                }))
                const getMaxArr = (times, timesDesc) => (times.length >= timesDesc.length) ? times : timesDesc;
                const mergeArr = (oldArr, arr) => oldArr.map((value, index) => ({
                    value,
                    ...arr[index]
                }))
                var list1 = f1('value', series);
                var list2 = f1('name', xAxis);
                var options =mergeArr(getMaxArr(series,xAxis), list2);
                option2.series[0].data = option2.series[0].data.concat(options);
                option3.xAxis["data"] = xAxis;
                option3.series[0]['data'] = series;
                resolve();
            } else {
                alert("error:HTTP状态码为:" + xmlHttpRequest.status);
                reject();
            }
        }
    }
})

ajaxHttpRequestFunc.then(function () {
    var myChart1 = echarts.init(document.getElementById('main-bing'));
    var myChart2 = echarts.init(document.getElementById('main-shu'));
    myChart1.setOption(option2);
    myChart2.setOption(option3);

})

